home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-25 | 5.0 KB | 242 lines | [TEXT/MPS ] |
- // Copyright: © 1993 Apple Computer, Inc. All rights reserved.
- // Author: Scott Searle (original)
- // Victor J. Hnyp (extensions)
- // Date: 03/13/93
-
- // Revisions
- //
- // 03/13/93 VJH 2.06 Fixed the way string compare works in EQString
- //
- // 03/11/93 VJH 2.05 Fixed the way string compare "matches" works
- //
- // 03/11/93 VJH 2.04 Changes to the way string compare "matches" works
- //
- // 02/08/93 VJH 2.03 Changes to the way GetNextItemAddress works
- //
- // 01/18/93 VJH 2.02 Added: N2S and GetNextItemAddress
- //
- // 01/04/93 VJH 2.01 Changed Copyright Notice, added new functions
-
- #include "AllHeaders.h"
- #include "Utility.h"
- #include "Context.h"
-
- // UTILITY PROTOTYPES
- OSErr SetContextResult(void* theData, Size theSize, void* outMessage, Size* outSize);
- Boolean CompareStringSpec(unsigned char* s1, StringSpecPtr stringSpec);
- Boolean EQString(register unsigned char* s1, register unsigned char* s2);
- Boolean TrapAvailable(short theTrap);
- Boolean StrStartsWith(unsigned char* s1, unsigned char* s2);
- Boolean StrEndsWith(unsigned char* s1, unsigned char* s2);
- Boolean StrContains(unsigned char* targetStr, unsigned char* searchStr);
- OSErr GetTheString(short id, unsigned char* str);
- StringPtr N2S( const long n );
- unsigned char* GetNextItemAddress( StringPtr theString );
-
- OSErr SetContextResult(void* theData, Size theSize, Ptr* outMessage, Size* outSize)
- {
- Ptr p;
-
- if (p = NewPtr(theSize))
- {
- BlockMove(theData, p, theSize);
-
- *outSize = theSize;
- *outMessage = p;
-
- return(noErr);
- }
- else
- return(MemError());
- }
-
- Boolean CompareStringSpec(unsigned char* s1, StringSpecPtr stringSpec)
- {
- Str255 test1, test2;
- unsigned char i;
-
- BlockMove(s1, test1, (long)(*s1 + 1));
- BlockMove(&stringSpec->str, test2, (long)(stringSpec->str[0] + 1));
-
- switch (stringSpec->method)
- {
- case containsAnything:
- return(*test1);
- break;
-
- case empty:
- return(!*test1);
- break;
-
- case matches:
- UprString(test1, false);
- UprString(test2, false);
- for (i = 0; i <= test1[0]; i++)
- if (test1[i] != test2[i])
- return( false );
- return( true );
- break;
-
- case contains:
- return(StrContains(test1, test2));
- break;
-
- case startsWith:
- return(StrStartsWith(test1, test2));
- break;
-
- case endsWith:
- return(StrEndsWith(test1, test2));
- break;
-
- default:
- return( false );
- break;
- }
- }
-
- // Compare s1 with s2; return true if they match exactly
- Boolean EQString(register unsigned char* s1, register unsigned char* s2)
- {
- register unsigned char i;
- Str255 str1, str2;
-
- BlockMove(s1, str1, (long)(*s1 + 1));
- BlockMove(s2, str2, (long)(*s2 + 1));
-
- UprString(str1, false);
- UprString(str2, false);
-
- for (i = 0; i <= str1[0]; i++)
- if (str1[i] != str2[i])
- return(false);
-
- return(true);
- }
-
- // Returns true if the trap is implemented
- Boolean TrapAvailable(short theTrap)
- {
- TrapType tType = GetTrapType(theTrap);
-
- if (tType == ToolTrap)
- {
- theTrap = theTrap & 0x07FF;
-
- if (theTrap >= NumToolboxTraps)
- theTrap = _Unimplemented;
- }
-
- return(NGetTrapAddress(theTrap, tType) != NGetTrapAddress(_Unimplemented, ToolTrap));
- }
-
- // True if s1 starts with s2
- Boolean StrStartsWith(unsigned char* s1, unsigned char* s2)
- {
- register unsigned char i;
-
- UprString(s1, false);
- UprString(s2, false);
-
- if (*s2 > *s1)
- return(false);
-
- for (i = 1; i <= *s2; ++i)
- if (s1[i] != s2[i])
- return(false);
-
- return(true);
- }
-
- // True if s1 ends with s2
- Boolean StrEndsWith(unsigned char* s1, unsigned char* s2)
- {
- UprString(s1, false);
- UprString(s2, false);
-
- if (*s2 > *s1)
- return(false);
-
- return(IUMagString(s1 + (*s1 - *s2) + 1, s2 + 1, (long)*s2, (long)*s2) == 0);
- }
-
- // True if targetStr contains searchStr
- Boolean StrContains(unsigned char* targetStr, unsigned char* searchStr)
- {
- register unsigned char i, j, t;
- register Boolean flag;
-
- UprString(targetStr, false);
- UprString(searchStr, false);
-
- if (*targetStr < *searchStr)
- return(false);
-
- for (i = 1; i <= *targetStr; ++i)
- {
- j = 1;
- if (searchStr[j] == targetStr[i] && j <= *searchStr && i + (*searchStr - 1) <= *targetStr)
- {
- flag = true;
- for (j = 2, t = i+1; j <= *searchStr && t <= *targetStr; ++j, ++t)
- if ((searchStr[j] != targetStr[t]))
- {
- flag = false;
- break;
- }
-
- if (flag)
- return(true);
- }
- }
-
- return(false);
- }
-
-
-
- OSErr GetTheString(short id, unsigned char* str)
- {
- StringHandle sHdl;
- short oldRef = CurResFile();
- OSErr err = noErr;
-
- UseResFile(0);
-
- if (sHdl = Get1Resource('STR ', id))
- {
- BlockMove(*sHdl, str, (long)(**sHdl + 1));
- ReleaseResource(sHdl);
- }
- else
- err = ResError();
-
- UseResFile(oldRef);
- return(err);
- }
-
-
-
- StringPtr N2S( const long n )
- /* Returns the string equivalent of a number */
- {
- static Str255 theStr;
-
- NumToString( n, theStr );
- return &theStr;
- } /* N2S */
-
-
-
- unsigned char* GetNextItemAddress( StringPtr theString )
- /* Given a pointer to the first string in the message, returns a pointer
- to the next Long (1st field following the 1st string in the message) */
- {
- unsigned char* aCharPtr;
-
- aCharPtr = theString + theString[0] + 1;
- if( (long)aCharPtr % 2 )
- aCharPtr += 1;
-
- return( aCharPtr );
- } // GetNextItemAddress